#!/bin/bash
#description: Auto install lnmp enviroment on centos7.3

SOFTDIR=/usr/local/src
USER="www"
GROUP="www"

N_SOFT="nginx-1.12.2.tar.gz"
N_SOFT_DIR="nginx-1.12.2"
N_URL="http://nginx.org/download/"
N_PREFIX="/usr/local/nginx-1.12.2"
N_CACHE="/var/cache/nginx"

M_SOFT="mysql-5.6.38-linux-glibc2.12-x86_64.tar.gz"
M_SOFT_DIR="mysql-5.6.38-linux-glibc2.12-x86_64"
M_URL="https://cdn.mysql.com//Downloads/MySQL-5.6/"
M_PREFIX="/usr/local/"

P_SOFT="php-5.6.33.tar.gz"
P_SOFT_DIR="php-5.6.33"
P_URL="http://cn2.php.net/distributions/"
P_PREFIX="/usr/local/php5"

function init(){
	[ ! -d $SOFTDIR ] && mkdir $SOFTDIR
	if ! id $USER &>/dev/null;then
		groupadd -g 3350 www
		useradd -u 3350 -g www -s /sbin/nologin www
	fi
	cd /etc/yum.repos.d/ && [ ! -f epel-7.repo ] && wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
	yum -y install gcc gcc-c++ glibc make autoconf cmake 

}
function install_nginx(){
	yum -y install gd-devel gd zlib-devel zlib openssl openssl-devel geoip geoip-devel
	#install pcre
	cd $SOFTDIR
	[ ! -f pcre-8.40.tar.gz ] && wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
	[ ! -d pcre-8.40 ] && tar xf pcre-8.40.tar.gz && cd pcre-8.40 && ./configure --prefix=/usr/local/pcre-8.40 && make && make install
	#install zlib
	cd $SOFTDIR
	[ ! -f zlib-1.2.11.tar.xz ] && wget http://www.zlib.net/zlib-1.2.11.tar.xz
	[ ! -d zlib-1.2.11 ] && tar xf zlib-1.2.11.tar.xz && cd zlib-1.2.11 && ./configure --prefxi=/usr/local/zlib-1.2.11 && make && make install
	#install nginx
	[ ! -d ${N_CACHE} ] && mkdir -p ${N_CACHE}
	[ ! -f ${N_SOFT} ] && wget ${N_URL}/${N_SOFT}
	[ ! -d ${N_SOFT_DIR} ] && tar xf ${N_SOFT}
	cd ${N_SOFT_DIR} && ./configure --prefix=${N_PREFIX} --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp  --user=www --group=www --with-file-aio --with-threads  --with-http_addition_module --with-http_auth_request_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_realip_module --with-http_secure_link_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module  --with-stream --with-stream_ssl_module  --with-http_image_filter_module --with-pcre=/usr/local/src/pcre-8.40 --with-zlib=/usr/local/src/zlib-1.2.11
	if [ $? -ne 0 ];then
		echo "nginx configure fail"
		exit 2
	fi
	make && make install
	if [ $? -ne 0 ];then
                echo "nginx install fail"
                exit 2
        fi
	#start nginx service
	${N_PREFIX}/sbin/nginx
}
function install_php(){
	yum -y install libmcrypt libmcrypt-devel mcrypt mhash mysql-devel swig libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel libcurl libcurl-devel zlib-devel
	#install php
	cd $SOFTDIR
	[ ! -f ${P_SOFT} ] && wget ${P_URL}/${P_SOFT}
	[ ! -d ${P_SOFT_DIR} ] && tar xf ${P_SOFT} && cd ${P_SOFT_DIR}
	./configure --prefix=${P_PREFIX} --with-pdo-mysql=mysqlnd  --with-mcrypt --with-mhash --enable-pdo --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd  --with-jpeg-dir --with-png-dir --with-zlib --enable-xml  --with-libxml-dir --with-curl --enable-bcmath --enable-shmop --enable-sysvsem  --enable-inline-optimization --enable-mbregex --with-openssl --enable-mbstring --with-gd --enable-gd-native-ttf --with-freetype-dir=/usr/lib64 --with-gettext=/usr/lib64 --enable-sockets --with-xmlrpc --enable-zip --enable-soap --disable-debug --enable-opcache --enable-zip --with-config-file-path=${P_PREFIX}/etc --enable-fpm --with-fpm-user=${USER} --with-fpm-group=${GROUP}
	if [ $? -ne 0 ];then
		echo "php configure fail"
		exit 2
	fi
	make &&  make install
	if [ $? -ne 0 ];then
		echo "php install fail"
		exit 2
	fi
	#configure php
	cd ${P_PREFIX}/etc
	[ ! -f php.ini ] && cp ${SOFTDIR}/${P_SOFT_DIR}/php.ini-production ${P_PREFIX}/etc/php.ini
	[ ! -f php-fpm.conf ] && cp php-fpm.conf.default php-fpm.conf
	cp ${SOFTDIR}/${P_SOFT_DIR}/sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
	chmod +x /etc/rc.d/init.d/php-fpm
	sed -i -e "s#pm.max_children = 5#pm.max_children = 50#" -e "s#pm.start_servers = 2#pm.start_servers = 5#" -e "s#pm.min_spare_servers = 1#pm.min_spare_servers = 2#" -e "s#pm.max_spare_servers = 3#pm.max_spare_servers = 8#" -e "s#;pid = run/php-fpm.pid#pid = run/php-fpm.pid#" ${P_PREFIX}/etc/php-fpm.conf
	#start php
	service php-fpm start
}
function install_mysql(){
	M_USER="mysql"
	M_GROUP="mysql"
	M_DATA="/mysql/data"
	yum -y install libaio libaio-devel
	if ! id ${M_USER} &>/dev/null;then
		groupadd -g 3260 mysql
		useradd -u 3260 -g mysql -s /sbin/nologin mysql
	fi
	[ ! -d ${M_DATA} ] && mkdir -p ${M_DATA}
	cd $SOFTDIR
	[ ! -f ${M_SOFT} ] && wget ${M_URL}/${M_SOFT}
	cd ${M_PREFIX} && [ ! -d ${M_SOFT_DIR} ] && tar xf ${SOFTDIR}/${M_SOFT} -C ${M_PREFIX}/ && ln -s ${M_PREFIX}/${M_SOFT_DIR}/   ${M_PREFIX}/mysql
	chown -R root.${M_GROUP} ${M_PREFIX}/mysql
	#配置my.cnf
	cat > /etc/my.cnf <<EOF
	[mysqld]
port = 3306
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
datadir=${M_DATA}
innodb_log_file_size = 1G
log_error = error.log
skip_name_resolve = 1
EOF
	#初始化mysql
	cd ${M_PREFIX}/mysql &&  ./scripts/mysql_install_db --user=$USER --datadir=${M_DATA}
	chown -R ${M_USER}:${M_GROUP} ${M_DATA}
	#配置mysql
	cd ${M_PREFIX}/mysql  &&  cp support-files/mysql.server /etc/init.d/mysqld
	chmod +x /etc/init.d/mysqld
	echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile.d/mysqld
	source /etc/profile.d/mysqld
	#start mysql
	service mysqld start
}
function main(){
	init
	install_nginx
	install_php
#	install_mysql
}
main
